<template>
  <div class="device-defect">
    <div class="breadcrumb-container">
      <!-- Breadcrumb 面包屑导航 -->
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>人员管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="header">
      <h1 class="title">人员管理</h1>
    </div>

    <!-- 查询结果部分 -->
    <div class="result-container">
      <h2 class="result-title">查询结果</h2>
      <el-table :data="list" style="width: 100%" >
        <!-- 表格列定义 -->

        <el-table-column prop="id" label="id"></el-table-column>
        <el-table-column prop="name" label="用户名"></el-table-column>

        <el-table-column v-if="checkPermission()" label="权限" width="180">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
            >
              删除
            </el-button>
            <el-select
              v-model="scope.row.state"
              placeholder="请选择状态"
              style="width: 100px; margin-right: 10px"
              @change="changeStatus(scope.row)"
            >
              <el-option label="admin1" value="admin1"></el-option>
              <el-option label="superuser" value="superuser"></el-option>
              <el-option label="user" value="user"></el-option>
            </el-select>
          </template>
        </el-table-column>
      </el-table>

    </div>
  </div>
</template>
<script>
import axios from "axios";

export default {
  name: "AdminSetting",
  data() {
    return {
      list: [{}], // 所有数据
     
    };
  },
  mounted() {
    this.list = [];
    this.m1();
  },
  methods: {
    checkPermission() {
      let user = localStorage.getItem("user");

      if (user) {
        let userObj = JSON.parse(user);
        let permission = userObj.permission;

        // 核查权限是否为 'admin1'
        if (permission === "superuser") {
          console.log("User has admin1 permission");
          return true
        } else {
          return false;
        }
      }
    },
    changeStatus(row) {
      axios.post("http://localhost:9527/user/updatePermission", {
        id: row.id, // row.id 应该是记录的唯一标识
        state: row.state,
      });
    },
   
    m1() {
      
      axios
        .get("http://localhost:9527/user/users")
        .then((data) => {
          this.list = data.data.data;
          console.log(data);
        })
        .catch((error) => {
          console.error("post请求出错", error);
        });
    },

  },
};
</script>

<style scoped>
.device-defect {
  padding: 20px;
}

.breadcrumb-container {
  margin-bottom: 20px;
}

.header {
  text-align: center;
  margin-bottom: 20px;
}

.title {
  font-size: 24px;
  margin: 0;
}

.query-form {
  margin-bottom: 20px;
}

.result-container {
  margin-top: 20px;
}

.result-title {
  font-size: 20px;
  margin-bottom: 10px;
}

/* 可以继续添加其他样式来自定义界面 */
</style>
